<template>
    <view style="background-color: #fff;position: fixed;width: 100%;height: 100%;">
        <view style="display:flex;justify-content:space-around;padding:12rpx;">
            <text class="recom" :class="[isShow==0?'active':'']" @click="choose(0)">推荐话题</text>
            <text class="recom" :class="[isShow==1?'active':'']" @click="choose(1)">自定义话题</text>
        </view>
        <view style="padding: 32rpx;" v-if="isShow==1">
            <view class="feature">
                <view class="add" @click="openPopup()">
                    <image style="width: 24rpx;height: 24rpx;margin-right: 6rpx;" src="https://www.diandianyouke.site:801/fileDataBase/static/stag/static/img/mov_icon_tjiazse.png" mode="" />
                    <text>添加新话题</text>
                </view>
                <text style="font-size: 28rpx;" :class="{'flag':isFlag}" @click="getIsFlag">{{isFlag?'完成':'管理'}}</text>
            </view>
            <view 
            class="items1" 
            v-for="item,index in diyLabel" 
            :key="index" 
            :class="{'choose-active':diyLabelArr.includes(item.labelName)&&!isFlag}"
            @click="checked(item.labelName)"
            >
                <view class="top">
                    <text class="text-top" style="color: rgb(69,70,74);">话题{{ index+1 }}</text>
                    <view class="btn" v-if="isFlag">
                        <button class="edit" @click="editLabel(item)">编辑</button>
                        <button class="del" @click="delDiyLabel(item.id)">删除</button>
                    </view>
                </view>
                <text class="text-bottom">{{ item.labelName }}</text>
                <image v-if="diyLabelArr.includes(item.labelName)&&!isFlag" class="img" src="https://www.diandianyouke.site:801/fileDataBase/static/stag/static/img/icon_xzong.png"/>
            </view>
            <!-- <view class="items" v-for="item,index in diyLabel" :key="index" :class="[diyLabelArr.includes(item.labelName)?'checked':'']" @click="checked(item.labelName)">
                <text class='items-text'>{{item.labelName}}</text>
                <image @click="delDiyLabel(item.id)" style="width: 48rpx;height: 48rpx;" src="https://www.diandianyouke.site:801/fileDataBase/static/stag/static/img/button_shanchu.png"/>
            </view>
            <button class="btn" @click="openPopup()">添加自定义话题</button> -->

        </view>
        <view v-if="isShow==0" style="padding: 32rpx;margin-bottom: 32rpx;">
            <checkbox-group @change="change">
            <label v-for="item,index in labelArr" :key="index" class="radio">
                <text>{{item.labelName}}</text>
                <checkbox color="rgba(142,84,244,1)" :value="item.labelName"/>
            </label>
            </checkbox-group>
        </view>
    </view>
    <view style="height: 150rpx;">
        <view class="bottom">
                <button style="width: 690rpx;" class="bottom-queren" @click="queren()">确认</button>
        </view>
    </view>
    <uni-popup ref="popup" background-color="rgba(241,241,241,1)">
				<view class="popup-content">
                    <text class="text">自定义标题</text>
                    <input v-model="labelName" class="input" placeholder="请输入自定义话题"/>
                </view>
                <view class="bottom">
                <button class="bottom-quxiao" @click="popupClose()">取消</button>
                <button class="bottom-queren" @click="addDiyLabel()">确认</button>
            </view>
	</uni-popup>
</template>

<script>
import { httpRequest } from '../../utils/http';
export default {
    name: 'UniPresetVueVite1Topic',

    data() {
        return {
          shopInfo:{},
          labelArr:[],
          diyLabel:[],
          labelName:'',
          isShow:0,
          changeArr:[],
          diyLabelArr:[],
          isChecked:0,
          isFlag:false,
          edit:false,
          item:{}
        };
    },
    onLoad(e) {
        this.shopInfo = uni.getStorageSync('shop')[e.index]
        this.getLabel()
        this.getDiyLabel()
    },
    methods: {
        editLabel(e){
            this.edit = true
            this.item =e
            this.labelName=e.labelName
           this.$refs.popup.open('bottom')
        },
        getIsFlag(){
            this.isFlag = !this.isFlag
        },
        checked(i){
            console.log(this.diyLabelArr.includes(i));
            if(this.diyLabelArr.includes(i)){
               this.diyLabelArr = this.diyLabelArr.filter(item => item!=i)
               return
            }
            this.diyLabelArr.push(i) 
        },
        change(e){
            console.log(e.detail.value);
            this.changeArr = e.detail.value
            
        },
        openPopup(){
            this.$refs.popup.open('bottom')
        },
        choose(n){
            this.isShow = n
        },
        popupClose(){
        this.$refs.popup.close()
        this.labelName = ''
    },
        //获取推荐话题
       async getLabel(){
        const res = await httpRequest('/api/labelRad','POST',{shopCate2Id:this.shopInfo.shopCate2Obj.shopCate2Id})
        this.labelArr = res.data
        },
        //获取自定义话题
        async getDiyLabel(){
            const res = await httpRequest('/api/getDiyLabel','POST',{shopId:this.shopInfo.shopId})
            console.log(res);
            this.diyLabel = res.data
            
        },
        //新增自定义话题
        async addDiyLabel(){
            if(this.edit){
            uni.request({
                url: 'https://www.diandianyouke.cn:8081/api/editDiyLabel',
                method: 'POST',
                data: {
                    shopId:this.item.shopId,
                    labelName:this.labelName,
                    id:this.item.id
                },
                header:{
                    "Content-Type":'application/json'
                },
                success: (res) => {
                    console.log(res);
                    uni.showToast({
                        title: '修改成功',
                        icon: 'none'
                    })
                    this.getDiyLabel()
                    this.$refs.popup.close()
                },
                fail: () => {
                    uni.showToast({
                        title: '修改失败',
                        icon: 'none'
                    })
                },
            })
           
        }else{
            const res = await httpRequest('/api/addDiyLabel','POST',{
                labelName:this.labelName,
                shopId:this.shopInfo.shopId
            })
            this.getDiyLabel()
            this.labelName = ''
            this.$refs.popup.close()
        }
        },
        //删除自定义话题
        async delDiyLabel(id){
           await httpRequest('/api/delDiyLabel','POST',{
                id:id
            })
            this.getDiyLabel()
        },
        queren(){
            uni.setStorageSync('label',[...this.changeArr,...this.diyLabelArr])
            uni.navigateBack({ delta: 1 })
        }
    },
};
</script>

<style lang="scss" scoped>
.checked{
    border: 1px solid rgba(142,84,244,1);
}
.recom{
        font-size: 28rpx;
        height: 110rpx;
        line-height: 110rpx;
        color: rgb(144,147,153);
    }
.items{
    height: 96rpx;
    padding: 16rpx;
    margin-bottom: 16rpx;
    background-color: rgba(142,84,244,0.1);
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-radius: 8rpx;
    .items-text{
        font-size: 28rpx;
        color: rgba(142,84,244,1);
    }
   
}
// .btn{
//     width: 686rpx;
//     height: 96rpx;
//     border-radius: 8rpx;
//     border: 2rpx dashed rgba(142,84,244,1);
//     text-align: center;
//     line-height: 96rpx;
//     margin-top: 20rpx;
// }
.popup-content{
    height: 648rpx;
    padding: 24rpx 32rpx;
    .text{
        font-size: 32rpx;
        height: 96rpx;
        font-weight: 700;
    }
    .input{
        margin-top: 50rpx;
        background-color: #fff;
        height: 170rpx;
        width: 100%;
        border-radius: 16rpx;
    }
}
.radio{
    height: 88rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 28rpx;
    padding: 26rpx 0;
    border-top: 1px solid rgba(220,223,230,1);
    border-bottom: 1px solid rgba(220,223,230,1);
}
.bottom{
    position: fixed;
    bottom: 34rpx;
    left: 0;
    display: flex;
    justify-content: space-between;
    width: 100%;
    .bottom-quxiao{
    width: 330rpx;
    height: 86rpx;
    text-align: center;
    line-height: 88rpx;
    color: rgba(142,84,244,1);
    border: 1px solid rgba(142,84,244,1);
}
.bottom-queren{
    width: 332rpx;
    height: 88rpx;
    text-align: center;
    line-height: 88rpx;
    background-color: rgba(142,84,244,1);
    color: #fff;
    margin-right: 30rpx;
}
    }
    .active{
        color: rgba(142,84,244,1);
        border-bottom: 2px solid rgba(142,84,244,1);
        font-size: 32rpx;
    }

    .items1{
    position: relative;
    padding: 24rpx;
    background-color: rgba(249,249,249,1);
    border-radius: 16rpx;
    margin-top: 24rpx;
    display: flex;
    flex-direction: column;
    border: 1px solid rgba(249,249,249,1);
    .text-top{
        font-size: 32rpx;
        color: rgba(255,55,71,1);
        font-weight: 700;
        margin-bottom: 12rpx;
    }
    .text-bottom{
        font-size: 28rpx;
        font-weight: 400;
        color: rgba(69,70,74,1);
    }
    .img{
        position: absolute;
        top: 0;
        right: 0;
        width: 72rpx;
        height: 48rpx;
    }
    .top{
        display: flex;
        justify-content: space-between;
        .btn{
            display: flex;
            justify-content: flex-end;
            .edit{
                width: 88rpx;
                height: 48rpx;
                text-align: center;
                line-height: 48rpx;
                font-size: 24rpx;
                color: rgba(69,70,74,1);
                background-color: rgb(241,241,241);
                padding: 0;
                margin: 0;
                margin-right: 24rpx;
            }
            .del{
                width: 88rpx;
                height: 48rpx;
                text-align: center;
                line-height: 48rpx;
                font-size: 24rpx;
                color: rgba(255,55,71,1);
                background-color: rgba(255,55,71,0.05);
                padding: 0;
                margin: 0;
            }
        }
    }
}
.feature{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 30rpx;
    .add{
        display: flex;
        justify-content: center;
        align-items: center;
        width: 252rpx;
        height: 64rpx;
        border: 2rpx solid rgba(142,84,244,1);
        font-size: 30rpx;
        color: rgba(142,84,244,1);
        border-radius: 8rpx;
    }
}
.flag{
    color: rgba(255,55,71,1) !important;
}
.choose-active{
    border: 1px solid rgba(142,84,244,1);
    background-color: rgba(142,84,244,0.05);
}
.choose-custom{
    border: 1px solid rgba(142,84,244,1);
}
</style>